package com.api.gbhg.liuwenbo.utils;

import com.api.gbhg.liuwenbo.dao.DataBaseUtil;
import com.google.common.base.Joiner;
import weaver.conn.RecordSet;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class AlgorithmUtil {



    /**
     *  实时执行率
     * @param Divisor1 除数
     * @param Divisor2 被除数
     * @return
     */
    public static String getRealTimeImplementRate(double Divisor1,double Divisor2){
        if(Divisor2 > 0){
            return FormatUtil.DmFormat(100-(Divisor1 / Divisor2 * 100));
        }else{
            return FormatUtil.DmFormat(0);
        }
    }


    /**
     * 年度执行率
     * @param year 年
     * @param ze 金额
     * @return
     */
    public static String getYearAvgImplRate(int year,double ze){
        LocalDate now = LocalDate.now();
        if(ze > 0) {
            if (year == now.getYear()) {
                int day = now.getDayOfYear();
                return FormatUtil.DmFormat(day / ze * 100);
            } else {
                int day = LocalDate.of(year, 1, 1).getDayOfYear();
                return FormatUtil.DmFormat(day / ze * 100);
            }
        }else{
            return FormatUtil.DmFormat(0);
        }
    }

    /**
     * 根据分部id获取所有下级分部
     * @param id 分部id
     * @return
     */
    private static List<Integer> getSubordinate(Integer id){
        RecordSet subComRs = DataBaseUtil.getSubComIdBySupId(id);
        List<Integer> list = new ArrayList<>();
        while (subComRs.next()){
            list.add(subComRs.getInt("id"));
            List<Integer> integers = getSubordinate(subComRs.getInt("id"));
            if(integers.size()>0)
                list.addAll(integers);
        }
        return list;
    }

    /**
     * 根据单位id获取下级单位
     * @param subId  分部id
     * @return
     */
    public static String getSubCompanIdBybmId(Integer subId){
        if (subId==2){
            return "2"; //拱北海关
        }else{
            List<Integer> list = AlgorithmUtil.getSubordinate(subId);
            list.add(subId);
            return Joiner.on(",").join((Iterable<?>) list);
        }
    }
}
